package telas.sistema;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.MaskFormatter;

import testes.textfieldnumerico.TextFieldNumerico;

public class ListenerJTextField implements DocumentListener{

	JTextField tf;
	
	public ListenerJTextField(JTextField tf) {
		this.tf = tf;
	}

	@Override
	public void changedUpdate(DocumentEvent e) {

	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub

		String str = tf.getText();

		if(str != null) {

			for( int i = 0; i < str.length(); i++ )  
				if( Character.isDigit( str.charAt( i ) ) == false ) {
					tf.setBackground(Color.RED);
				}

		}
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub

		String str = tf.getText();
		boolean possuiLetras = false;
		
		if(str.length() >= 1) {

			for( int i = 0; i < str.length(); i++ )  {
				if( Character.isDigit(str.charAt(i)) == false ) {
					possuiLetras = true;
					
				}
			}	
		}   
		
		
		if(possuiLetras) tf.setBackground(Color.RED);
		else             tf.setBackground(Color.WHITE);
	}
}